/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.gemom.combinationItems;

import com.gemom.*;
import com.gemom.util.*;
import java.util.*;

import org.apache.commons.math.distribution.NormalDistributionImpl;
import org.apache.commons.math.MathException;

/**
 *
 * @author Yair Diaz
 */
public class BidGene {
    
    private Item[] itemsArray;
    private GBroker[] gBrokersArray;
    private CovMatrixData covM;
    private Vector<Bid> listBids;
    
    public BidGene(Item[] arrayItems,GBroker[] gBrokersArray,CovMatrixData covM )
    {
        this.itemsArray=arrayItems;
        this.gBrokersArray=gBrokersArray;
        this.covM=covM;
     
        Vector<Vector<Item>>[]combisItems= new Vector[gBrokersArray.length];
        
        for(int i=0;i<gBrokersArray.length;i++)
        {
            SearchCombinations brokerCombis1 = new SearchCombinations(itemsArray,
                                                this.gBrokersArray[i].getBrokerRate() );
            combisItems[i]= brokerCombis1.getCombinations();
            brokerCombis1.printCombinations();
            System.out.println(brokerCombis1.getNumberCombinations());
            System.out.println("Visits: "+brokerCombis1.visits);
       }
        
        
       //Generating the candidatebids		
       Vector<Bid> listCandBids = new Vector<Bid>();
          
        
       int bidIdcounter=0; 
       //how to calculate price? for each combis....for all brokers
       for(int k=0;k<combisItems.length;k++)
       {
         
       //how to calculate price? for each combis..here for first Gbroker
        for(int i=0;i<combisItems[k].size();i++)
        {
        //here i add a new bid
        listCandBids.add(new Bid(bidIdcounter++));/////////////////////  
         
        double pro=0;
        int[]itemIDs= new int[combisItems[k].get(i).size()];//this is for calcVARCOVs
        double RS=0;double mean=0;
        for(int j=0;j<combisItems[k].get(i).size();j++)
        {
            RS+=combisItems[k].get(i).get(j).getRevenueItem();
            mean+=combisItems[k].get(i).get(j).getItemRate();
            itemIDs[j]=combisItems[k].get(i).get(j).getItemID();
            
            //here items are added to the bid
             // c.o.m.b.i.s.I.t.e.m.s[k].get(i).get(j)....this represents the item
            listCandBids.lastElement().addItem(combisItems[k].get(i).get(j));

            
        }
        double varian =covM.calculateVarsetItems(itemIDs);
        double sDev= Math.sqrt(varian);
        NormalDistributionImpl normalprob = new NormalDistributionImpl(mean,sDev);
        try{pro=normalprob.cumulativeProbability(this.gBrokersArray[k].getBrokerRate());}///k here? each broker
        catch(MathException e){System.out.println("pilas");pro=1;}
        double price=RS +(3*RS*pro); 
        
        //here price for current bid is set, and gbrokerid is set
        listCandBids.lastElement().price=price;
        listCandBids.lastElement().setBrokerIdFlag(this.gBrokersArray[k].getBrokerID());
        
   /*   
        System.out.println("RS=   "+RS);
        System.out.println("mean= "+mean);
        System.out.println("brokerC= "+gBrokersArray[k].getBrokerRate());
        System.out.print("itemIDs= ");
        for(int t=0;t<itemIDs.length;t++)
            System.out.print(itemIDs[t]+",");
        System.out.println("\n"+"Varia= "+varian);
        System.out.println("sDev=  "+sDev);
        System.out.println("probab=  "+pro);
        System.out.println("price=  "+price);
    */   
       }//end inner loop
     }//end outer loop
        
            
        
  //  for(int i=0;i<listCandBids.size();i++)
  //  {System.out.println(listCandBids.get(i).price);}
 ///////////////////////////////////////////////////////
 
       //randomising listBids vector
       Random ran= new Random();
       Vector<Bid> listBidsTemp =new Vector<Bid>();
       Vector<Bid> listCandBidscopy = (Vector<Bid>)listCandBids.clone();
       while(!listCandBidscopy.isEmpty())
       {   int index=ran.nextInt(listCandBidscopy.size());
           listBidsTemp.add(listCandBidscopy.get(index));
           listCandBidscopy.removeElement(listCandBidscopy.get(index));
       }
       listCandBids=listBidsTemp;
       ///////////////////////////////end randomisation
       
       
       
       
      //Generating the bids		
        listBids = new Vector<Bid>();    
        Vector<Bid> temp = new Vector<Bid>(); 
        Bid first;
        Random r= new Random();
        while(!listCandBids.isEmpty())
        {
            first =listCandBids.firstElement();
            listCandBids.removeElement(listCandBids.firstElement());
        
            for(int i=0;i<listCandBids.size();i++)
            {
               if(first.getItemsVec().size()==listCandBids.get(i).getItemsVec().size())
                {
                 int itemcount=0;
                 for(int k=0;k<first.getItemsVec().size();k++)
                     if(first.getItemsVec().get(k).equals(listCandBids.get(i).getItemsVec().get(k)))
                          itemcount++;
                 if(itemcount==first.getItemsVec().size())temp.add(listCandBids.get(i));
                }
            }
            for(int i=0;i<temp.size();i++)listCandBids.remove(temp.get(i));
            
        
            for(int i=0;i<temp.size();i++)
            {
                if(first.price<temp.get(i).price) first=temp.get(i);
            
                if(first.price==temp.get(i).price)
                    if(r.nextBoolean())
                        first=temp.get(i);
            }
        
            listBids.add(first);
             temp.removeAllElements();
        }//end of while loop
   

        int ff=0;
        for(int i=0;i<listBids.size();i++)
        {
            System.out.println(listBids.get(i).price +"  "+(ff++)+"  "+listBids.get(i).getBrokerIdFlag());
        }  
      
    //  for(int i=0;i<listBids.size();i++)
 //     {
  //        System.out.println(listBids.get(i));
  //    }
      
        int a=0;int b=0;int c=0;int d=0;int e=0;
       for(int i=0;i<listBids.size();i++)
      {
        if(gBrokersArray[0].getBrokerID()==listBids.get(i).getBrokerIdFlag())a++;
        if(gBrokersArray[1].getBrokerID()==listBids.get(i).getBrokerIdFlag())b++;
        if(gBrokersArray[2].getBrokerID()==listBids.get(i).getBrokerIdFlag())c++;
        if(gBrokersArray[3].getBrokerID()==listBids.get(i).getBrokerIdFlag())d++;
        if(gBrokersArray[4].getBrokerID()==listBids.get(i).getBrokerIdFlag())e++;

      }
        
       System.out.println(a);           
           System.out.println(b);
            System.out.println(c);
             System.out.println(d);
              System.out.println(e); 
   
    
    System.out.println("number bids= "+listBids.size()); 
    
    }

    
    public Vector<Bid> getlistBids(){return this.listBids;}


}
